"""
接口文档
https://openapi-portal.sheincorp.cn/#/
shein请求结果解析模块
功能说明：从shein请求到的数据，解析成PB需要的数据
url:请求地址，区分不同请求的唯一标识
res:请求返回的结果
"""


def ParseResult(url, res):
    if url == "/open-api/order/purchase-order-infos":
        # 订单明细
        return PurchaseOrderInfos(res)

    if url == "/open-api/goods/number-list":
        # 商品接口-全量查询SKC/SKU/设计款号关系列表
        print("GoodsNumberList")
        return GoodsNumberList(res)

    if url == "/open-api/goods/skc":
        # 商品接口-根据SKC/SKU查询SKC与尺码、SKU、设计款号关系
        return GoodsSkc(res)

    if url == "/open-api/order/storage-receiver-info":
        # 订单接口-查询SHEIN仓库的收件信息
        return StorageReceiverInfo(res)

    if url == "/open-api/stock/access":
        # 库存接口-实时库存同步
        return StockAccess(res)


def PurchaseOrderInfos(res):
    """
    返回数据结构

    {
        'code': '0', 
        'msg': '',
        'info': {
            'pageNo': 1,
            'count': 80899,
            'pageSize': 20, 
            'list': [
                {
                    'orderNo': 'B210128000104', 
                    'typeName': '备货',        
                    ...
                    'orderExtends': [
                        {'skc': 'swear18200904669',
                        'size': '',             
                        ...
                        }
                    ]
                }     
                ...
            ]
        }
    }
    """

    if type(res) != dict:
        return {"code": 0, "data": "返回的结果不是字典类型"}

    if "info" not in res:
        return {"code": 0, "data": "结果中没有找到info键"}

    if "list" not in res["info"]:
        return {"code": 0, "data": "结果中没有找到list键"}

    rowHeader = ""
    for k in res["info"]["list"][0].keys():
        rowHeader += str(k) + "\t"

    for k in res["info"]["list"][0]["orderExtends"][0].keys():
        rowHeader += str(k) + "\t"

    content = rowHeader + "\n"

    for item in res["info"]["list"]:
        rowItemHeader = ""
        for v in item.values():
            if type(v) != list:
                rowItemHeader += str(v) + "\t"

        for detail in item["orderExtends"]:
            rowItemDetail = rowItemHeader
            for v in detail.values():
                rowItemDetail += str(v) + "\t"
            rowItemDetail += "\n"
            content += rowItemDetail

    # print("11111111111111111111")
    # print(content)
    return {"code": 1, "data": content}


def GoodsNumberList(res):
    """
    {
        'code': 0,
        'msg': 'ok',
        'info': {
            'page': 1,
            'per_page': 20,
            'count': 254,
            'list': [{
                'skc': '',
                'size': 'one-size',
                'supplier_sku': '',
                'design_code': 'M03202101230003'
            }, {
                'skc': '',
                'size': '',
                'supplier_sku': '3',
                'design_code': 'M02202101230005'
            }, 
            ...
        }
    }
    """

    if type(res) != dict:
        return {"code": 0, "data": "返回的结果不是字典类型"}

    if "info" not in res:
        return {"code": 0, "data": "结果中没有找到info键"}

    if "list" not in res["info"]:
        return {"code": 0, "data": "结果中没有找到list键"}

    rowHeader = ""
    for k in res["info"]["list"][0].keys():
        rowHeader += str(k) + "\t"

    content = rowHeader + "\n"

    for item in res["info"]["list"]:
        rowItemDetail = ""
        for v in item.values():
            rowItemDetail += str(v) + "\t"
        rowItemDetail += "\n"
        content += rowItemDetail

    return {"code": 1, "data": content}


def GoodsSkc(res):
    print(res)
    pass


def StorageReceiverInfo(res):
    pass


def StockAccess(res):
    pass
